package com.jk.controller;

import com.alibaba.druid.util.StringUtils;
import com.jk.bean.Author;
import com.jk.bean.Content;
import com.jk.bean.HuiFu;
import com.jk.bean.PingLun;
import com.jk.service.TaRenZhuYeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 骚骚的🐏
 * @Author: 杨
 * @Date: 2019/3/11 14:52
 */
@Controller
@RequestMapping("taren")
public class TaRenZhuYeController {
    @Resource
    private TaRenZhuYeService taRenZhuYeService;
    @Autowired
    private MongoTemplate  mongoTemplate;
    @RequestMapping("queryAuthor")
    @ResponseBody
    public List<Author> queryAuthor(String id){

        List<Author> list=taRenZhuYeService.queryAuthor(id);
        return list;
    }
    @RequestMapping("queryContent")
    @ResponseBody
    public List<Content> queryContent(String id){
        Query query = new Query();
        if(!StringUtils.isEmpty(id)) {
            query.addCriteria(Criteria.where("authorid").is(id));
        }
        List<Content> cn= mongoTemplate.find(query, Content.class);
        return  cn;
    }
 @RequestMapping("queryXiangQing")
    @ResponseBody
    public Content queryXiangQing(String id , String nameid){
        Query query = new Query();
        if(!StringUtils.isEmpty(id)) {
            query.addCriteria(Criteria.where("authorid").is(id));
        }
         if(!StringUtils.isEmpty(nameid)) {
             query.addCriteria(Criteria.where("nameid").is(nameid));
         }
        Content cn= mongoTemplate.findOne(query,Content.class);
        return  cn;
    }


    @RequestMapping("queryPingLun")
    @ResponseBody
    public List<PingLun> queryPingLun(String id , String nameid){
       List<PingLun> list= taRenZhuYeService.queryPingLun(id,nameid);
        return list;
    }
    @RequestMapping("queryHuiFu")
    @ResponseBody
    public List<HuiFu> queryHuiFu(String pinglunid){
       List<HuiFu> list= taRenZhuYeService.queryHuiFu(pinglunid);
        return list;
    }
    @RequestMapping("addPingLun")
    @ResponseBody
    public String addPingLun(PingLun pingLun){
        List<String> a = taRenZhuYeService.queryStv();

        boolean sd = false;

        for (String s : a) {
            if (pingLun.getPingtext().trim().contains(s.trim())) {
                sd = true;
                break;
            }

        }

        if (sd) {
            return "1";
        } else {
            taRenZhuYeService.addPingLun(pingLun);

            return "2";
        }
    }
   @RequestMapping("addHuiFu")
    @ResponseBody
    public String addHuiFu(HuiFu huiFu){
        //先根据 评论人对的id查出名字和图片

       List<String> a = taRenZhuYeService.queryStv();

       boolean sd = false;

       for (String s : a) {
           if (huiFu.getHuifutext().trim().contains(s.trim())) {
               sd = true;
               return "1";
           }

       }

           try {
               Author au= taRenZhuYeService.queryPinglunren(huiFu.getPinglunrenid());
               huiFu.setPinglunrenimg(au.getAimage());
               huiFu.setPinglunrenname(au.getAname());
               taRenZhuYeService.addHuiFu(huiFu);
           }catch (Exception e){
               System.out.println("代码失误");
               return "2";
           }

        return "";
    }



}
